{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "89564cd3",
   "metadata": {},
   "source": [
    "1. lightgb导包建模时无需传参\n",
    "2. df_oh.corr() corr['G3'].sort_values(ascending=False)[:9] 要加倒序,sort value没有by\n",
    "3. 注意scoring,model_gs = GridSearchCV(estimator=model,param_grid=param_grids,cv=5,scoring=make_scorer(mean_absolute_error))\n",
    "4. ereg = VotingRegressor([('gb',reg1),('rf',reg2),('lr',reg3)],weights=[1,5,10]) weights别忘了\n",
    "5.mae,rmse,abs 是回归的，看到这个就知道是回归\n",
    "6.使用lightBGM算法进行建模，并使用网格搜索对模型进行优化cv使用5，输出模型评分的mae,rmse,abs 是先对lightBGM进行评分，然后进行网格搜索"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8a7d9a39",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')\n",
    "df = pd.read_csv('./data/student-info.csv')\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "02ec1375",
   "metadata": {},
   "source": [
    "1.查看年龄和性别的分布柱状图,输出如下:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0eb65a77",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "#由考生填写\n",
    "plt.figure()\n",
    "sns.countplot(x='age',hue='sex',data=df)\n",
    "plt.xlabel('age')\n",
    "plt.ylabel('sex')\n",
    "plt.title('Distribution bar chart of age and gender')\n",
    "plt.show()\n",
    "#由考生填写"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "48c54f4c",
   "metadata": {},
   "source": [
    "2.查看年龄和成绩分布箱线图，输出如下:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7974bd85",
   "metadata": {},
   "outputs": [],
   "source": [
    "#由考生填写\n",
    "plt.figure()\n",
    "sns.boxplot(x='age',y='G3',data=df)\n",
    "plt.xlabel('age')\n",
    "plt.ylabel('G3')\n",
    "plt.title('Box plot of age and Grade distribution')\n",
    "#由考生填写"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8dcbcf15",
   "metadata": {},
   "source": [
    "3.使用swarmplot函数查看年龄和成绩分布图;输出如下:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5c6bacc3",
   "metadata": {},
   "outputs": [],
   "source": [
    "#由考生填写\n",
    "plt.figure()\n",
    "sns.swarmplot(x='age',y='G3',data=df)\n",
    "plt.xlabel('age')\n",
    "plt.ylabel('G3')\n",
    "plt.title('Age and grade distribution chart')\n",
    "#由考生填写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "67084a54",
   "metadata": {},
   "outputs": [],
   "source": [
    "del df['G1']\n",
    "del df['G2']\n",
    "df_oh = pd.get_dummies(df)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7bc51e2b",
   "metadata": {},
   "source": [
    "4.分析数据相关性系数，并取出和G3相关性最高的9个属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "723e2693",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "#由考生填写\n",
    "corr = df_oh.corr()\n",
    "corr['G3'].sort_values(ascending=False)[:9]\n",
    "#由考生填写\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "5963e0ad",
   "metadata": {},
   "outputs": [],
   "source": [
    "X = df_oh.loc[:,df_oh.columns !='G3']\n",
    "y = df_oh['G3']\n",
    "from sklearn.model_selection import train_test_split\n",
    "X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.3, random_state=42)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8d465727",
   "metadata": {},
   "source": [
    "5.使用lightBGM算法进行建模，并使用网格搜索对模型进行优化cv使用5，输出模型评分的mae,rmse,abs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "fe6f9767",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.867060883042273\n",
      "3.4821981842139205\n",
      "0.26555636973017954\n"
     ]
    }
   ],
   "source": [
    "#由考生填写\n",
    "from lightgbm import LGBMRegressor\n",
    "model = LGBMRegressor()\n",
    "model.fit(X_train,y_train)\n",
    "from sklearn.metrics import mean_absolute_error,mean_squared_log_error,mean_squared_error\n",
    "y_pre = model.predict(X_test)\n",
    "print(mean_absolute_error(y_test,y_pre))\n",
    "print(np.sqrt(mean_squared_error(y_test,y_pre)))\n",
    "print(np.median(np.abs((y_test-y_pre)/y_pre)))\n",
    "#由考生填写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3a56f063",
   "metadata": {},
   "outputs": [],
   "source": [
    "#由考生填写\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "from sklearn.metrics import make_scorer\n",
    "param_grids = {'learn-rate':[0.1,0.2,0.3,0.4,0.5]}\n",
    "model_gs = GridSearchCV(estimator=model,param_grid=param_grids,cv=5,scoring=make_scorer(mean_absolute_error))\n",
    "model_gs.fit(X_train,y_train)\n",
    "#由考生填写"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2ac11e4d",
   "metadata": {},
   "source": [
    "6.使用Voting 算法聚合RandomForestRegressor,GradientBoostingRegressor,LinearRegression模型，预测成绩G3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "09a6bdce",
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "from sklearn.datasets import load_diabetes\n",
    "from sklearn.ensemble import GradientBoostingRegressor\n",
    "from sklearn.ensemble import RandomForestRegressor\n",
    "from sklearn.linear_model import LinearRegression\n",
    "from sklearn.ensemble import VotingRegressor\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import r2_score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "fb63aae2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "VotingRegressor R2: gbdt 0.0463064102401235\n",
      "VotingRegressor R2: rf 0.10576604911069498\n",
      "VotingRegressor R2: lr -0.10878212911773932\n",
      "VotingRegressor R2: voting 3.8035881562148433e-06\n"
     ]
    }
   ],
   "source": [
    "reg1 = GradientBoostingRegressor(random_state=1)\n",
    "reg2 = RandomForestRegressor(random_state=1)\n",
    "reg3 = LinearRegression()\n",
    "#由考生填写\n",
    "ereg = VotingRegressor(estimators=[('gbdt',reg1),('rf',reg2),('lr',reg3)],weights=[1,5,10])\n",
    "for reg,label in zip([reg1,reg2,reg3,ereg],['gbdt','rf','lr','voting']):\n",
    "    reg.fit(X_train,y_train)\n",
    "    y_pred = reg.predict(X_test)\n",
    "    print('VotingRegressor R2:',label,r2_score(y_test,y_pred))\n",
    "#由考生填写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ef32a2b8",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "PySpark-2.4.5",
   "language": "python",
   "name": "pyspark-2.4.5"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
